package com.gjy.redis.aspect;

import com.gjy.redis.annotation.MethodExecutionTime;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.time.Instant;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-13 15:20:30
 */
@Order(100)
@Aspect
@Component
public class MethodExecutionTimeAspect {

    private static final Logger log = LoggerFactory.getLogger(MethodExecutionTimeAspect.class);

    @Around("@annotation(methodExecutionTime)")
    public Object run(ProceedingJoinPoint joinPoint, MethodExecutionTime methodExecutionTime) throws Throwable {
        long start = Instant.now().toEpochMilli();

        Object proceed = joinPoint.proceed();

        long end = Instant.now().toEpochMilli();
        log.info("{} 执行时间: {} 毫秒", joinPoint.getSignature().toShortString(), (end - start));
        return proceed;
    }
}
